﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Task16
{
    class GraphNode
    {
        public int Value;
        public bool isVisited;
        List<GraphNode> previous;
        List<GraphNode> next;

        public List<GraphNode> Previous
        {
            get
            {
                if (previous == null)
                {
                    previous = new List<GraphNode>();
                }
                return previous;
            }
        }

        public List<GraphNode> Next
        {
            get
            {
                if (next == null)
                {
                    next = new List<GraphNode>();
                }
                return next;
            }
        }

        public GraphNode(int value)
        {
            Value = value;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            Dictionary<int, GraphNode> graph = new Dictionary<int, GraphNode>();

            for (int i = 0; i < n; i++)
            {
                string input = Console.ReadLine();
                string[] inputArr = input.Split(' ');
                int first = int.Parse(inputArr[0]);
                int second = int.Parse(inputArr[1]);

                if (!graph.ContainsKey(first))
                {
                    graph.Add(first, new GraphNode(first));
                }
                if (!graph.ContainsKey(second))
                {
                    graph.Add(second, new GraphNode(second));
                }

                GraphNode node;
                GraphNode depend;

                graph.TryGetValue(second, out node);
                graph.TryGetValue(first, out depend);

                node.Previous.Add(depend);
                depend.Next.Add(node);
            }

            var nodes = from p in graph.Values
                        where p.Previous.Count == 0
                        select p;
            if (nodes.Count() == 0)
            {
                Console.WriteLine("Not valid");
                return;
            }

            Queue<GraphNode> toVisit = new Queue<GraphNode>();

            foreach (GraphNode tempNode in nodes)
            {
                toVisit.Enqueue(tempNode);
            }
            int visitedCount = 0;

            List<int> result = new List<int>();
            while (toVisit.Count > 0)
            {
                GraphNode currentNode = toVisit.Dequeue();
                bool previousVisited = (from p in currentNode.Previous
                                        where p.isVisited == false
                                        select p).Count() == 0;
                if (currentNode.Previous.Count == 0 || previousVisited)
                {
                    if (!currentNode.isVisited)
                    {
                        result.Add(currentNode.Value);
                        visitedCount++;
                    }

                    currentNode.isVisited = true;
                    foreach (GraphNode tempNode in currentNode.Next)
                    {
                        toVisit.Enqueue(tempNode);
                    }
                }
            }

            if (visitedCount < graph.Count())
            {
                Console.WriteLine("Not valid");
            }
            else
            {
                for (int i = 0; i < result.Count; i++)
                {
                    if (i < result.Count - 1)
                    {
                        Console.Write("{0}, ", result[i]);
                    }
                    else
                    {
                        Console.WriteLine(result[i]);
                    }
                }
            }
        }
    }
}
